package com.zking.ssm.DYM.controller;

import com.zking.ssm.DYM.model.HotelCoupons;
import com.zking.ssm.DYM.service.IHotelCouponsService;
import com.zking.ssm.util.JsonData;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author XXXXX
 * @Date 2021/01/23 8:58
 */
@Controller
@RequestMapping("/hotelCoupons")
public class HotelCouponsController {
    @Autowired
    private IHotelCouponsService hotelCouponsService;

    @ModelAttribute
    public void init(Model model) {
        System.out.println("init");
        HotelCoupons hotelCoupons = new HotelCoupons();
        model.addAttribute("hotelCoupons", hotelCoupons);
    }

    //添加数据
    @RequestMapping("/toAddHotelCoupons")
    public String toAddHotelCoupons(Model model) {
        System.out.println("toAddHotelCoupons");
        return "addHotelCoupons";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(HotelCoupons hotelCoupons) {
        System.out.println("add");
        hotelCouponsService.insert(hotelCoupons);
        return "redirect:toAddHotelCoupons";
    }

    //修改数据
    @RequestMapping("/updateHotelCoupons")
    public String toUpdateHotelCoupons(@PathVariable("id") Long id, Model model) {
        model.addAttribute("updateHotelCoupons", hotelCouponsService.selectByPrimaryKey(1L));
        return "updateHotelCoupons";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(HotelCoupons hotelCoupons) {
        hotelCouponsService.updateByPrimaryKey(hotelCoupons);
        return "redirect:updateHotelCoupons";
    }

    //删除
    @RequestMapping(value = "/deleteHotelCouponsById")
    @ResponseBody
    public String deleteHotelCoupons(Long id) {
        hotelCouponsService.deleteByPrimaryKey(id);
        System.out.println(id);
        return "redirect:HotelCoupons";
//        return "删除成功";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonData list(HotelCoupons hotelCoupons, PageBean pageBean) {
        List<HotelCoupons> hotelCouponsList = hotelCouponsService.list(hotelCoupons, pageBean);
        System.out.println("集合：" + hotelCouponsList);
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setPage(pageBean.getPage());
        jsonData.setRows(pageBean.getRows());
        jsonData.setTotal(pageBean.getTotal());
        jsonData.setMessage("查询成功");
        jsonData.setResult(hotelCouponsList);
        return jsonData;
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public JsonData get(HotelCoupons hotelCoupons) {
        HotelCoupons h = hotelCouponsService.selectByPrimaryKey(hotelCoupons.getId());
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setMessage("查询成功");
        jsonData.setResult(h);
        return jsonData;
    }

}
